//////////////////////////////////////////////////////////////////////////
/// Sho
/// Ve ban co, quan co
//////////////////////////////////////////////////////////////////////////


#include "Othello.h"
#include "math.h"
#include "ResourceManager.h"

#define BOARD_HEIGHT 8	//Chieu dai ban co
#define BOARD_WIDTH 8	//Chieu rong ban co
#define BOARD_UNIT 1	//Kich thuoc 1 o co
#define CHESS_RADIUS 0.4f	//Ban kinh quan co
const double PI = atan(1.0)*4;

/// Khoi tao Doi tuong Othello
Othello::Othello()
{
	/// Khoi tao Matrix 8x8 quan ly 64 quan co
	m_chessArray = new int*[BOARD_HEIGHT];
	for(int i = 0; i < BOARD_HEIGHT; i++) 
		m_chessArray[i] = new int[BOARD_HEIGHT];
	for(int i = 0 ; i < BOARD_HEIGHT; i++)
		for(int j = 0;j < BOARD_HEIGHT; j++)
			m_chessArray[i][j] = 0;

	/// Bat dau van choi voi 4 quan co dat giua
	/// ETurn la 1 enum (coi o Othello.h)
	m_chessArray[3][3] = ETurn::HUMAN;
	m_chessArray[3][4] = ETurn::COMPUTER;
	m_chessArray[4][3] = ETurn::COMPUTER;
	m_chessArray[4][4] = ETurn::HUMAN;

}
Othello::~Othello()
{

}

/// Ve mat phang (ban co) khong thay doi trong suot cuoc doi cua no 
GLvoid Othello::DrawBoard()
{
	int i, j;
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);

	/// Load texture
	ResourceManager* resourceMng = resourceMng->GetInst();
	resourceMng->LoadTexture(Leo);
	
	glTranslatef(m_boardPos.X, m_boardPos.Y, m_boardPos.Z);

	for(i = 0; i < BOARD_HEIGHT; i += BOARD_UNIT)
	{
		for(j = 0; j < BOARD_WIDTH; j += BOARD_UNIT)
		{
			/// ve ban co 64 o, voi tung o xen ke mau nhau
			if(i % 2 == 0) 
			{
				if(j % 2 == 0) 
					glColor4f(1, 0.38f, 0.27f, 1);
				else 
					glColor4f(1, 0.62f, 0.47f, 1);
			}
			else
			{
				if(j % 2 != 0) 
					glColor4f(1, 0.38f, 0.27f, 1);
				else 
					glColor4f(1, 0.62f, 0.47f, 1);
			}

			glBegin(GL_QUADS);

			glTexCoord2f(1 - (float) i / BOARD_HEIGHT, 1 - (float) j / BOARD_WIDTH);
			glVertex3f(i, 0, j);

			glTexCoord2f(1 - (float) i / BOARD_HEIGHT, 1 - (float) (j + BOARD_UNIT) / BOARD_WIDTH); 
			glVertex3f(i, 0, j + BOARD_UNIT);

			glTexCoord2f(1 - (float) (i + BOARD_UNIT) / BOARD_HEIGHT, 1 - (float) (j + BOARD_UNIT) / BOARD_WIDTH); 
			glVertex3f(i + BOARD_UNIT, 0, j + BOARD_UNIT);

			glTexCoord2f(1 - (float) (i + BOARD_UNIT) / BOARD_HEIGHT, 1 - (float) j / BOARD_WIDTH);
			glVertex3f(i + BOARD_UNIT, 0, j);
			glEnd();
		}
	}

	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}

/// Ve 1 hinh tron voi ban kinh r
GLvoid DrawCircle(float r)
{
	glPushMatrix();
	glTranslatef(0, 0, 0);
	glBegin(GL_TRIANGLE_FAN);
	glVertex3f(0, 0, 0);
	for(float i = 0; i <= 360; i += 10)
	{
		glVertex3f(r*cos(i*PI/180), 0, r*sin(i * PI / 180));
	}
	glEnd();
	glPopMatrix();
}

/// ve hinh tru giua 2 mat cua quan co, ban kinh r
GLvoid DrawDepth(float r)
{
	glPushMatrix();
	glTranslatef(0, 0, 0);
	glColor3f( 0, 0.52f, 0.4f);
	glBegin(GL_QUAD_STRIP);
	for(float i = 0; i <= 360; i += 10)
	{
		glColor3f( 0, 0.52f, 0.4f);
		glVertex3f(r * cos(i * PI / 180), 0, r * sin(i * PI / 180));
		glColor3f( 0.86f, 0.07f, 0.3f);
		glVertex3f(r * cos(i * PI / 180), -0.2f, r * sin(i * PI / 180));
	}
	glEnd();
	glPopMatrix();
}

/// Ve quan co [x][z], voi luot di m_turn
GLvoid Othello::DrawChess(float x,float z,int m_turn)
{
	glEnable(GL_DEPTH_TEST);	
	if(m_turn == ETurn::NONE) 
		return;
	glPushMatrix();

	/// thay doi x, z de xac dinh vi tri tam quan co
	x += (float) BOARD_UNIT / 2;
	z += (float) BOARD_UNIT / 2;

	glTranslatef(x, 0.01f, z);
	glDepthMask(true);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	if(m_turn == ETurn::COMPUTER) //neu la computer thi lat nguoc quan co
	{
		glColor4f( 0, 0.52f, 0.54f, 0.7f);//mat tren (xanh)

		DrawCircle(CHESS_RADIUS);//ve mau mau hong

		glTranslatef(0, 0.2f, 0);
		glColor4f( 0.86f, 0.07f, 0.3f, 0.7f);//mat duoi (hong)
		DrawCircle(CHESS_RADIUS);//ve mat mau xanh
		glRotatef(180, 0, 0, 1);
		glTranslatef(0, 0.2f, 0);
		DrawDepth(CHESS_RADIUS);//ve do day cua quan co
	}
	else
	{

		glColor4f( 0.86f, 0.07f, 0.3f, 0.7f);//mat duoi (hong)
		DrawCircle(CHESS_RADIUS);//ve mau mau hong

		glTranslatef(0, 0.2f, 0);
		glColor4f( 0, 0.52f, 0.54f, 0.7f);//mat tren (xanh)
		DrawCircle(CHESS_RADIUS);//ve mat mau xanh
		DrawDepth(CHESS_RADIUS);//ve do day cua quan co
	}
	glDisable(GL_BLEND);
	glPopMatrix();
	
}

/// Ham update cac phan tu cua mang, va ve tung quan co len
void Othello::UpdateBoard()
{
	/// Duyet het ma tran
	for(int i = 0; i < BOARD_HEIGHT; i++)
		for(int j = 0; j < BOARD_HEIGHT; j++)
		{
			/// Ve quan co theo luot choi
			if(m_chessArray[i][j] == ETurn::HUMAN)
			{
				DrawChess(i, j, ETurn::HUMAN);
				
			}
			if(m_chessArray[i][j] == ETurn::COMPUTER) 
			{
				DrawChess(i, j, ETurn::COMPUTER);
			}
		}
}







